<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of config
 *
 * @author b4by
 */
class config extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('model_config');
	}

	public function index() {
		$error = array();
		$list = $this->model_config->get_list(array(
			'select' => 'name,value'
				));
		$configs = array();
		foreach ($list as &$config) {
//			if($config->name!='footer' && $config->name!='online_support_desc' ) 
//				$configs[$config->name] = htmlspecialchars($config->value);
//			else 
				$configs[$config->name] = $config->value;
		}
//		pr($configs);die;
		$filter = array('image_upload_path', 'site_title', 'meta_desc', 'fb_image', 'social_page',
			'image_max_size', 'image_max_width', 'image_max_height', 'image_width', 'image_height',
			'thumb_width', 'thumb_height', 'keywords','favicon','footer','online_support',
			'footer_links','online_support_desc','youtube');
		if ($this->input->post('submit')) {

			$data = request_filter($filter);

			if ($this->isFormValid()) {
//				echo $data['footer_links'];die;
				$this->model_config->update($data);
				$this->data['state'] = 'success';
				$this->data['message'] = lang('config_updated');
				echo json_encode(array(
					'state' => 'success',
					'message' => lang('config_saved')
				));
			} else {
				$this->data['state'] = 'success';
				$this->data['message'] = lang('config_update_fail');
				foreach ($data as $name => $value) {
					$error[$name] = form_error($name);
				}
				echo json_encode(array(
					'state' => 'error',
					'errors' => $this->data['errors']
				));
			}

			foreach ($data as $name => $value) {
				$configs[$name] = $value;
			}
			exit();
		}
//		pr($configs);die;
//		$this->data['data'] = request_filter($filter, $configs);
		$this->data['data'] = $configs;
		$this->data['error'] = $error;
		$this->data['temp'] = 'admin/config/index';
		$this->load->view('admin/main', $this->data);
	}

	protected function isFormValid() {
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('image_upload_path', 'lang:image_upload_path', 'required');
		$this->form_validation->set_rules('site_title', 'lang:site_title', 'required');
//		$this->form_validation->set_rules('fb_image', 'lang:fb_image', 'required');
		$this->form_validation->set_rules('meta_desc', 'lang:site_title', 'required');
		$this->form_validation->set_rules('keywords', 'lang:keyword', 'required');
//		$this->form_validation->set_rules('counter_update_freq', 'lang:counter_update_freq', 'required|is_natural_no_zero');
//		$this->form_validation->set_rules('vote_pass', 'lang:vote_pass', 'required|is_natural_no_zero');
//		$this->form_validation->set_rules('time_old', 'lang:time_old', 'required|is_natural_no_zero');
		$this->form_validation->set_rules('image_width', 'lang:image_width', 'required|is_natural_no_zero');
		$this->form_validation->set_rules('image_height', 'lang:image_height', 'required|is_natural_no_zero');
		$this->form_validation->set_rules('image_max_width', 'lang:image_max_width', 'required|is_natural_no_zero');
		$this->form_validation->set_rules('image_max_height', 'lang:image_max_height', 'required|is_natural_no_zero');
		$this->form_validation->set_rules('thumb_width', 'lang:thumb_width', 'required|is_natural_no_zero');
		$this->form_validation->set_rules('thumb_height', 'lang:thumb_height', 'required|is_natural_no_zero');
//		$this->form_validation->set_rules('thumb2_width', 'lang:thumb2_width', 'required|is_natural_no_zero');
//		$this->form_validation->set_rules('thumb2_height', 'lang:thumb2_height', 'required|is_natural_no_zero');
		$this->form_validation->set_rules('image_max_size', 'lang:image_max_size', 'required|is_natural_no_zero');
		return $this->form_is_valid();
	}

}

?>
